package gameObjects;

import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Touchable;

public class Card extends Actor {

	CardTable table;
	Sprite cardImage;
	Sprite backImage;
	boolean showBack;
	SpriteBatch spriteBatch;
	float posX;
	float posY;
	int suit;
	int value;

	public Card(Sprite _sprite, Sprite _backImage, SpriteBatch _spriteBatch,
			int _suit, int _value, CardTable _table) {
		table = _table;
		showBack = false;
		backImage = _backImage;
		suit = _suit;
		value = _value;
		cardImage = _sprite;
		spriteBatch = _spriteBatch;
		posX = 0.0f;
		posY = 0.0f;
		this.setTouchable(Touchable.enabled);
		this.setPosition(posX, posY);

		this.setSize(cardImage.getWidth(), cardImage.getHeight());
		System.out.println("actor position: " + this.getX() + "  "
				+ this.getY());
		System.out.println("actor position: " + this.getWidth() + "  "
				+ this.getHeight());

		this.addListener(new InputListener() {
			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				System.out.println("card down");
				System.out.println("card suit: " + suit);
				System.out.println("card value: " + value);
				return true;
			}

			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {

				play();

			}

			@Override
			public void touchDragged(InputEvent event, float x, float y,
					int pointer) {
				// posX = x;
				// System.out.println("pos x: " + posX);
				// posY = y;
				// System.out.println("pos y: " + posY);
			}
		});
	}

	public int getSuit() {
		return suit;
	}

	public void play() {

		table.playCard(this);
	}

	public int getValue() {
		return value;
	}

	public void showBack(boolean show) {

		showBack = show;

	}

	@Override
	public void setPosition(float _posX, float _posY) {
		super.setPosition(_posX, _posY);
		posX = _posX;
		posY = _posY;
	}

	@Override
	public void act(float delta) {
		super.act(delta);
		this.setPosition(posX, posY);

		if (showBack) {
			backImage.setPosition(posX, posY);
			backImage.draw(spriteBatch);
		} else {
			cardImage.setPosition(posX, posY);
			cardImage.draw(spriteBatch);
		}
	}

}
